package com.oceanstar.service.Impl;

import com.oceanstar.Pojo.*;
import com.oceanstar.mapper.UserInfoMapper;
import com.oceanstar.mapper.ActivityMapper;
import com.oceanstar.service.IActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@Service
public class ActivityServiceImpl implements IActivityService {

    // 注入mapper
    private final ActivityMapper activityMapper;
    private final UserInfoMapper userInfoMapper;
    // 构造器注入
    public ActivityServiceImpl(ActivityMapper activityMapper, UserInfoMapper userInfoMapper) {
        this.activityMapper = activityMapper;
        this.userInfoMapper = userInfoMapper;
    }

    /*
     *创建新的活动并发布到系统
     */
    @Override
    public void publishActivity(ActivityInfo info, String token) {
        activityMapper.insertActivity(info);
    }

    /*
     *查询活动列表
     */
    @Override
    public PageInfo<ActivityDTO> getActivityList(int page, int pageSize, int status) {
        // 参数校验
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 1) {
            pageSize = 1;
        }

        // 计算分页偏移量
        Integer offset = (page - 1) * pageSize;

        // 查询当前页数据
        List<ActivityInfo> List = activityMapper.selectActivityList(offset, pageSize,status);

        //转换为ActivityDTO对象列表
        List<ActivityDTO> activityDTOSList = List.stream().map(activityInfo -> {
            ActivityDTO dto = new ActivityDTO();

            // 设置基本字段
            dto.setActivityId(activityInfo.getActivityId());
            dto.setTitle(activityInfo.getTitle());
            dto.setStartTime(activityInfo.getStartTime());
            dto.setLocation(activityInfo.getLocation());
            dto.setMaxParticipants(activityInfo.getMaxParticipants());
            dto.setContent(activityInfo.getContent());

            // 查询当前参与人数
            dto.setCurrentParticipants(activityMapper.countCurrentParticipants(dto.getActivityId()));

            return dto;
        }).toList();

        // 查询总记录数
        long total = activityMapper.countActivityList(status);
        // 构建并返回PageResult对象
        return new PageInfo<>(total, activityDTOSList);
    }

    /*
     *查看指定活动的报名人员清单。
     */
    @Override
    public PageInfo<ActivityParticipantsDTO> getParticipantsList(int activityId, int page, int pageSize) {
        // 参数校验
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 1) {
            pageSize = 1;
        }

        // 计算分页偏移量
        Integer offset = (page - 1) * pageSize;

        // 查询当前页数据
        List<ActivityParticipantsDTO> List = activityMapper.selectParticipantsList(offset, pageSize, activityId);

        //获取报名者信息
        List<ActivityParticipantsDTO> activityParticipantsDTOSList = List.stream().map(Participants -> {
            ActivityParticipantsDTO dto = new ActivityParticipantsDTO();

            // 设置基本字段
            UserInfo userInfo = userInfoMapper.selectById(Participants.getElderlyId());
            dto.setName(userInfo.getName());

            //计算老人年龄
            String birthDateStr = userInfo.getIdCard().substring(6, 14);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            LocalDate birthDate = LocalDate.parse(birthDateStr, formatter);
            LocalDate currentDate = LocalDate.now();
            dto.setAge(Period.between(birthDate, currentDate).getYears());

            if(userInfo.getGender() == 1)
                dto.setGender("男");
            else
                dto.setGender("女");

            return dto;
        }).toList();

        // 查询总记录数
        long total = activityMapper.countCurrentParticipants(activityId);
        // 构建并返回PageResult对象
        return new PageInfo<>(total, activityParticipantsDTOSList);
    }

    /*
     *为老人报名参加指定活动
     */
    @Override
    public void activitySignUp(ActivitySignUpInfo info, String token) throws Exception {
        if (activityMapper.selectSignUpInfo(info) != null)
            throw new Exception("已报名，请勿重复操作");
        ActivityInfo activityInfo = activityMapper.selectActivityById(info.getActivityId());
        if(activityInfo.getMaxParticipants() <= activityMapper.countCurrentParticipants(info.getActivityId()))
            throw new Exception("该活动报名人数已满");
        activityMapper.insertSignUpInfo(info);
    }

    /*
     *取消已报名的活动。
     */
    @Override
    public void activityCancel(ActivitySignUpInfo info, String token) {
        activityMapper.activityCancel(info);
    }


}
